package com.light.model;

import com.light.constant.Constant;
import com.light.util.SwingUtil;

import java.awt.*;

/**
 * @Author MoonlightL
 * @ClassName: Bird
 * @ProjectName flappy_bird
 * @Description: 像素鸟
 * @DateTime 2023/3/13, 0013 12:37
 */
public class Bird extends GameElement {

    private int originX;

    private int originY;

    private int index;

    private Image[] images = new Image[3];

    private double h = 0.0;

    private double t = 1;

    private double v = 0;

    private double angle = 0;

    public Bird(int x, int y) {
        super(x, y, SwingUtil.getImageIcon("img/bird1_0.png").getImage());
        this.originX = x;
        this.originY = y;

        for (int i = 0; i < 3; i++) {
            this.images[i] = SwingUtil.getImageIcon("img/bird1_" + i + ".png").getImage();
        }
    }

    @Override
    public void draw(Graphics2D g) {
        super.draw(g);
    }

    @Override
    public void reset() {
        this.setX(originX);
        this.setY(originY);
        this.h = 0;
        this.t = 1;
        this.v = 0;
    }

    public void fly() {
        if (index == 3) {
            index = 0;
        }
        this.setImage(this.images[index++]);
    }

    public void stop() {
        index = 0;
    }

    public void tap() {
        this.v = 30;
        this.angle = 30.0;
    }

    public void down() {
        v = v - Constant.g * t;
        h = v * t -  Constant.g * t * t / 2;
        int newY = this.getY() - (int) h;
        // 12 为图片的透明边框
        if (newY <= -12) {
            newY = -12;
        }

        if (newY >= Constant.LAND_Y + 12) {
            newY = Constant.LAND_Y - getHeight() / 2 - 12;
        }

        this.setY(newY);
    }

    public boolean checkHit() {
        return this.getY() <= -12 || this.getY() >= Constant.LAND_Y - getHeight() / 2 - 12;
    }

}
